﻿using ComponentFactory.Krypton.Toolkit;
using Nini.Config;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TfsHelper.WinFrontEnd
{
    public partial class frmTracking : KryptonForm
    {
        #region " Fields "

        private bool tracking;
        private int workitemId;
        private string title;
        private DateTime startTime;
        private double opacity = 0;

        private IConfigSource config = new XmlConfigSource(Application.StartupPath + "\\Config.xml");

        #endregion

        #region " Properties "

        public TimeSpan Time
        {
            get { return TimeSpan.Parse(lblTime.Text); }
        }

        public string TfsTime
        {
            get { return lblTimeRound.Text.Replace(" (TFS Time)", ""); }
        }

        public bool IsTracking
        {
            get { return tracking; }
        }

        public int WorkitemId
        {
            get { return workitemId; }
        }

        public bool IsMeeting { get; set; }

        public int MeetingId { get; set; }

        #endregion

        #region " Constructor "

        public frmTracking()
        {
            InitializeComponent();

            opacity = config.Configs["Appearance"].GetDouble("Transparency") / 100;
        }

        #endregion

        #region " Handlers "

        private void frmTracking_Load(object sender, EventArgs e)
        {
            int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
            int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
            this.Location = new Point(x, y);
        }

        private void frmTracking_Activated(object sender, EventArgs e)
        {
            Opacity = 1D;
        }

        private void frmTracking_Deactivate(object sender, EventArgs e)
        {
            Opacity = opacity;
        }

        private void frmTracking_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
                StopTracking(true);
        }

        private void timerTrack_Tick(object sender, EventArgs e)
        {
            var time = (DateTime.Now - startTime);
            var minutes = time.Minutes;

            var hours = minutes / 60;
            minutes = minutes % 60;

            lblTime.Text = time.Hours.ToString().PadLeft(2, '0') + ":" + minutes.ToString().PadLeft(2, '0');

            double round = (double)minutes / 60 * 100;
            minutes = Convert.ToInt32(Math.Round(round / 25, MidpointRounding.ToEven) * 25);

            if (minutes == 100)
                lblTimeRound.Text = (time.Hours + 1).ToString().PadLeft(2, '0') + ".00 (TFS Time)";
            else
                lblTimeRound.Text = time.Hours.ToString().PadLeft(2, '0') + "." + minutes.ToString().PadLeft(2, '0') + " (TFS Time)";
        }

        private void linkStop_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            StopTracking(true);
        }

        #endregion

        #region " Methods "

        public void StartTracking(int workitemId, string title)
        {
            this.workitemId = workitemId;
            this.title = title;

            tracking = true;
            timerTrack.Start();
            startTime = DateTime.Now;

            lblHeader.Text = workitemId + " - " + title;
            lblTime.Text = "00:00";
            lblTimeRound.Text = "00.00 (TFS Time)";

            TopMost = config.Configs["Appearance"].GetBoolean("TopMost");

            Show();
        }

        public void StopTracking(bool closeWindow)
        {
            timerTrack.Stop();
            SaveEffort();
            tracking = false;
            IsMeeting = false;
            if (closeWindow)
            {
                Close();
            }
            workitemId = 0;
            MeetingId = 0;
        }

        private void SaveEffort()
        {
            if (Time.TotalMinutes > 5)
            {
                using (var uof = new TfsHelper.Model.TfsModel())
                {
                    uof.Add(new Model.Time()
                    {
                        Start = startTime,
                        Finish = startTime.Add(Time),
                        WorkitemId = WorkitemId
                    });

                    uof.SaveChanges();
                }
            }
        }

        #endregion
    }
}
